<?php
/**
 * Clase Base DP para BaseImTransferDP
 *
 * @package    
 * @subpackage BaseImTransferDP
 * @author     hans
 * @version    SVN:: BaseImTransferDP.class.php,v 1.1 2012-06-08 23:43:21 Hans.parra Exp $
 */
require_once('class/im/transfer/BN/ImTransferBN.class.php');
abstract class BaseImTransferDP extends ImTransferBN
{
  /* N�mero de registros afectados */
  private $nRowsAffected;

  public function __construct($bSetDefault = true)
  {
    if( $bSetDefault instanceof ImTransferBN )
    {
      $o_ImTransferBN = $bSetDefault;
      if($o_ImTransferBN->getTransferId()) : $this->setTransferId($o_ImTransferBN->getTransferId()); endif;
      if($o_ImTransferBN->getDocTransfer()) : $this->setDocTransfer($o_ImTransferBN->getDocTransfer()); endif;
      if($o_ImTransferBN->getDocRef()) : $this->setDocRef($o_ImTransferBN->getDocRef()); endif;
      if($o_ImTransferBN->getAlmOrigenId()) : $this->setAlmOrigenId($o_ImTransferBN->getAlmOrigenId()); endif;
      if($o_ImTransferBN->getAlmDestinoId()) : $this->setAlmDestinoId($o_ImTransferBN->getAlmDestinoId()); endif;
      if($o_ImTransferBN->getTipoTransfer()) : $this->setTipoTransfer($o_ImTransferBN->getTipoTransfer()); endif;
      if($o_ImTransferBN->getEstado()) : $this->setEstado($o_ImTransferBN->getEstado()); endif;
      if($o_ImTransferBN->getObs()) : $this->setObs($o_ImTransferBN->getObs()); endif;
      if($o_ImTransferBN->getCreaUsuari()) : $this->setCreaUsuari($o_ImTransferBN->getCreaUsuari()); endif;
      if($o_ImTransferBN->getCreaFecha()) : $this->setCreaFecha($o_ImTransferBN->getCreaFecha()); endif;
      if($o_ImTransferBN->getModiUsr()) : $this->setModiUsr($o_ImTransferBN->getModiUsr()); endif;
      if($o_ImTransferBN->getModiFecha()) : $this->setModiFecha($o_ImTransferBN->getModiFecha()); endif;
    }
  }

  /* Obtiene el n�mero de registros afectados */
  public function getRowsAffected()
  {
    return $this->nRowsAffected;
  }

  public function save()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      
      $this->setTransferId($this->getSecuencia());

      if($this->getTransferId()){
        $a_Fields = array();

        if($this->getTransferId()) : $a_Fields['imt_id'] = ($this->getTransferId()); endif;
        if($this->getDocTransfer()) : $a_Fields['imt_docTransfer'] = ($this->getDocTransfer()); endif;
        if($this->getDocRef()) : $a_Fields['imt_docRef'] = ($this->getDocRef()); endif;
        if($this->getAlmOrigenId()) : $a_Fields['imt_almOrigenId'] = ($this->getAlmOrigenId()); endif;
        if($this->getAlmDestinoId()) : $a_Fields['imt_almDestinoId'] = ($this->getAlmDestinoId()); endif;
        if($this->getTipoTransfer()) : $a_Fields['imt_tipoTransfer'] = ($this->getTipoTransfer()); endif;
        if($this->getEstado()) : $a_Fields['imt_estado'] = ($this->getEstado()); endif;
        if($this->getObs()) : $a_Fields['imt_Obs'] = ($this->getObs()); endif;
        if($this->getCreaUsuari()) : $a_Fields['imt_creaUsr'] = ($this->getCreaUsuari()); endif;
        if($this->getCreaFecha()) : $a_Fields['imt_creaFecha'] = ($this->getCreaFecha()); endif;
        if($this->getModiUsr()) : $a_Fields['imt_modiUsr'] = ($this->getModiUsr()); endif;
        if($this->getModiFecha()) : $a_Fields['imt_modiFecha'] = ($this->getModiFecha()); endif;

        $sStringField = arrayClass::implode_keys(',', $a_Fields);
        $sStringValue = "'".implode("','", $a_Fields)."'";

        $sql = "INSERT INTO im_transfer (".$sStringField.") VALUES (".$sStringValue.");";
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows();
        }
        else
        {
          throw new Exception( mysql_error() );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function update()
  {
    try
    {
      if($this->getTransferId())
      {
        $a_Fields = array();

        if($this->getTransferId()) : $a_Fields['imt_id'] = ($this->getTransferId()); endif;
        if($this->getDocTransfer()) : $a_Fields['imt_docTransfer'] = ($this->getDocTransfer()); endif;
        if($this->getDocRef()) : $a_Fields['imt_docRef'] = ($this->getDocRef()); endif;
        if($this->getAlmOrigenId()) : $a_Fields['imt_almOrigenId'] = ($this->getAlmOrigenId()); endif;
        if($this->getAlmDestinoId()) : $a_Fields['imt_almDestinoId'] = ($this->getAlmDestinoId()); endif;
        if($this->getTipoTransfer()) : $a_Fields['imt_tipoTransfer'] = ($this->getTipoTransfer()); endif;
        if($this->getEstado()) : $a_Fields['imt_estado'] = ($this->getEstado()); endif;
        if($this->getObs()) : $a_Fields['imt_Obs'] = ($this->getObs()); endif;
        if($this->getCreaUsuari()) : $a_Fields['imt_creaUsr'] = ($this->getCreaUsuari()); endif;
        if($this->getCreaFecha()) : $a_Fields['imt_creaFecha'] = ($this->getCreaFecha()); endif;
        if($this->getModiUsr()) : $a_Fields['imt_modiUsr'] = ($this->getModiUsr()); endif;
        if($this->getModiFecha()) : $a_Fields['imt_modiFecha'] = ($this->getModiFecha()); endif;

        $sStringField = arrayClass::implode_key_value(',', $a_Fields);

        $o_conexion = new BaseDatos();
        $con = $o_conexion->conectar();
        $sql2 = "UPDATE im_transfer SET ".$sStringField." WHERE 1 AND imt_id='".$this->getTransferId()."';";
        $result = mysql_query($sql2);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows();
        }
        else
        {
          throw new Exception( mysql_error() );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function delete()
  {
    try
    {
      if($this->getTransferId())
      {
        $a_Fields = array();

        $o_conexion = new BaseDatos();
        $con = $o_conexion->conectar();
        $sql = "DELETE FROM im_transfer WHERE 1 AND imt_id='".$this->getTransferId()."';"; 
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($oLink);
        }
        else
        {
          throw new Exception( mysql_error($oLink) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  /* B�squeda del registro por la llave primaria */
  public static function selectByPK($vTransferId)
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT imt_id, imt_docTransfer, imt_docRef, imt_almOrigenId, imt_almDestinoId, imt_tipoTransfer, imt_estado, imt_Obs, imt_creaUsr, imt_creaFecha, imt_modiUsr, imt_modiFecha FROM im_transfer WHERE 1 AND imt_id='".$vTransferId."'; ";
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      
      return $o_Propel?self::insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_ImTransferDP = new ImTransferBN();
    $o_ImTransferDP->setTransferId($o_propel->imt_id);
    $o_ImTransferDP->setDocTransfer($o_propel->imt_docTransfer);
    $o_ImTransferDP->setDocRef($o_propel->imt_docRef);
    $o_ImTransferDP->setAlmOrigenId($o_propel->imt_almOrigenId);
    $o_ImTransferDP->setAlmDestinoId($o_propel->imt_almDestinoId);
    $o_ImTransferDP->setTipoTransfer($o_propel->imt_tipoTransfer);
    $o_ImTransferDP->setEstado($o_propel->imt_estado);
    $o_ImTransferDP->setObs($o_propel->imt_Obs);
    $o_ImTransferDP->setCreaUsuari($o_propel->imt_creaUsr);
    $o_ImTransferDP->setCreaFecha($o_propel->imt_creaFecha);
    $o_ImTransferDP->setModiUsr($o_propel->imt_modiUsr);
    $o_ImTransferDP->setModiFecha($o_propel->imt_modiFecha);
    return $o_ImTransferDP;
  }
}